package com.example.aiquestions.controller;

import com.example.aiquestions.entity.GraphData;
import com.example.aiquestions.mapper.SubjectMapper;
import com.example.aiquestions.model.common.ApiResponse;
import com.example.aiquestions.service.GraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/graph")
public class GraphController {

    @Autowired
    private GraphService graphService;
    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 获取所有图谱数据
     */
    @GetMapping("/list")
    public ApiResponse<List<GraphData>> getAllGraphData() {
        try {
            List<GraphData> graphDataList = graphService.getAllGraphData();
            return ApiResponse.success(graphDataList);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(500, "获取图谱数据失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取图谱数据
     */
    @GetMapping("/{id}")
    public ApiResponse<GraphData> getGraphDataById(@PathVariable Long id) {
        try {
            GraphData graphData = graphService.getGraphDataById(id);
            if (graphData == null) {
                return ApiResponse.error(404, "未找到指定ID的图谱数据");
            }
            return ApiResponse.success(graphData);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(500, "获取图谱数据失败：" + e.getMessage());
        }
    }

    /**
     * 根据科目ID获取图谱数据
     */
    @GetMapping("/subject/{subjectId}")
    public ApiResponse<List<GraphData>> getGraphDataBySubjectId(@PathVariable Long subjectId) {
        try {
            List<GraphData> graphDataList = graphService.getGraphDataBySubjectId(subjectId);
            String subjectName = subjectMapper.getSubjectById(subjectId).getName();
            for (GraphData graphData : graphDataList) {
                graphData.setSubjectName(subjectName);
            }
            return ApiResponse.success(graphDataList);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(500, "获取图谱数据失败：" + e.getMessage());
        }
    }

    /**
     * 新增图谱数据
     */
    @PostMapping("/add")
    public ApiResponse<Long> addGraphData(@RequestBody GraphData graphData) {
        try {
            Long id = graphService.addGraphData(graphData);
            return ApiResponse.success(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(500, "添加图谱数据失败：" + e.getMessage());
        }
    }

    /**
     * 批量新增图谱数据
     */
    @PostMapping("/batch/add")
    public ApiResponse<Integer> batchAddGraphData(@RequestBody List<GraphData> graphDataList) {
        try {
            int count = graphService.batchAddGraphData(graphDataList);
            return ApiResponse.success(count);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(500, "批量添加图谱数据失败：" + e.getMessage());
        }
    }

    /**
     * 更新图谱数据
     */
    @PutMapping("/update")
    public ApiResponse<Boolean> updateGraphData(@RequestBody GraphData graphData) {
        try {
            boolean success = graphService.updateGraphData(graphData);
            if (success) {
                return ApiResponse.success(true);
            } else {
                return ApiResponse.error(404, "未找到要更新的图谱数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(500, "更新图谱数据失败：" + e.getMessage());
        }
    }

    /**
     * 删除图谱数据
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Boolean> deleteGraphData(@PathVariable Long id) {
        try {
            boolean success = graphService.deleteGraphData(id);
            if (success) {
                return ApiResponse.success(true);
            } else {
                return ApiResponse.error(404, "未找到要删除的图谱数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(500, "删除图谱数据失败：" + e.getMessage());
        }
    }

    /**
     * 根据科目ID删除图谱数据
     */
    @DeleteMapping("/subject/{subjectId}")
    public ApiResponse<Integer> deleteGraphDataBySubjectId(@PathVariable Long subjectId) {
        try {
            int count = graphService.deleteGraphDataBySubjectId(subjectId);
            return ApiResponse.success(count);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(500, "删除图谱数据失败：" + e.getMessage());
        }
    }
}
